import React from 'react'
import { View, Image } from 'react-native'
import { Lt, Jx } from "../_utils"
import { Net, NET_NR } from "../_net"
import { eApp, eSort, cHome } from "../home/_home"
import { GoodsList } from "./ApplyGoodsList"
import { SortBar } from '../home/SortBar';

export class ApplyHomeSec extends React.PureComponent{
    constructor(props){
        super(props);

        const { app } = this.props;
        this.app = app;

        this.goodslist = null;
        // 第一项不是
        this.fieldIdx = 1;
        this.info = eSort.defaultInfo;
        this.fields = eSort.fields[app];
        this.fields[1] = "热度";

        this.onSort = this.onSort.bind(this);
        this.onLoad = this.onLoad.bind(this);
    }
    setFieldIdx(fieldIdx){
        this.fieldIdx = fieldIdx;
        this.refresh();
    }
    onSort(info){
        this.info = info;
        this.refresh();
    }
    refresh(){
        this.goodslist && this.goodslist.refresh();
    }
    onLoad(params){
        let obj = {
            type : this.app,
            keyword: "",
            ...cHome.getCatCid(this.app, this.fieldIdx),
            ...cHome.getSort(this.app, this.info),
            ...params.pagination,
        }
        Net.request("Shop/Search", obj, {params, NET_NR}).then(r=>{
            if(r.data){
                cHome.toFieldIdx(r.data.data, this.fieldIdx);
                params.add(r.data.data);
            }else{
                params.add();
            }
        });
    }
    render(){
        return <View style = {{flex:1}}>
            <SortBar
                fields = {this.fields}
                doubles = {eSort.doubles[this.app]}
                onSort={this.onSort}
            />
            <GoodsList
                ref={r=>this.goodslist=r}
                appIdx={eApp.rdxs[this.app]}
                onLoad={this.onLoad}
            />
        </View>
    }
}